package com.weilx959.spt.core.component;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import com.jfoenix.validation.base.ValidatorBase;
import com.weilx959.spt.core.StageContext;
import com.weilx959.spt.core.utils.FXMLUtil;
import com.weilx959.spt.core.utils.Try;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.stage.DirectoryChooser;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.Objects;

/**
 * 文件选择器表单项控件
 *
 * @author weilx
 * @since  2024年6月2日
 */
public class DirChooserFormItemControl extends ValidatorFormItemControl<String> {
    private static final String XML = "/assets/fxml/component/dir-chooser-form-control.fxml";

    @FXML
    private Label labelControl;

    @FXML
    private JFXTextField dictionaryControl;

    @FXML
    JFXButton browseControl;

    public DirChooserFormItemControl() {
        super();
        initialize();
    }

    @Override
    public boolean isModified() {
        return !StringUtils.equals(getValue(), oldValue);
    }

    @Override
    public Try<String> validate() {
        return Try.of(() -> {
            if (!dictionaryControl.validate()) {
                throw new IllegalStateException(dictionaryControl.getActiveValidator().getMessage());
            }
        });
    }

    @Override
    public void addValidator(ValidatorBase... validators) {
        dictionaryControl.setValidators(validators);
    }

    public EventHandler<ActionEvent> getOnAction() {
        return dictionaryControl.getOnAction();
    }

    public void setOnAction(EventHandler<ActionEvent> onAction) {
        dictionaryControl.setOnAction(onAction);
    }

    private void initialize() {
        // load
        FXMLUtil.loadControl(this, XML);
        // 输入框`
        dictionaryControl.textProperty().bindBidirectional(valueProperty());
    }

    /**
     * 打开文件选择器
     *
     * @param event 事件
     */
    @FXML
    private void openDirectoryChooserAction(ActionEvent event) {
        event.consume();
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("选择文件夹");
        File file = directoryChooser.showDialog(StageContext.get());

        if (Objects.isNull(file) || file.isFile() || !file.exists()) {
            dictionaryControl.setText("");
        } else {
            dictionaryControl.setText(file.getAbsolutePath());
        }
    }

    // builder
    public static DirChooserFormItemControlBuilder builder() {
        return new DirChooserFormItemControlBuilder();
    }

    public static class DirChooserFormItemControlBuilder
        extends ValidatorFormItemControlBuilder<String, DirChooserFormItemControl, DirChooserFormItemControlBuilder> {
        @Override
        protected DirChooserFormItemControl newFormItemControl() {
            return new DirChooserFormItemControl();
        }

        @Override
        protected DirChooserFormItemControlBuilder self() {
            return this;
        }
    }
}
